package com.knife.blog.logic.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.Photo;
import com.knife.blog.domain.PhotoDir;
import com.knife.blog.logic.HTMLGenerater;
import com.knife.blog.logic.PhotoService;
import com.knife.blog.util.BlogFileUtil;
import com.knife.web.Globals;
import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;
import org.apache.log4j.Logger;

public class PhotoServiceImpl extends DAOSupportService implements PhotoService {
    private static final Logger logger = (Logger) Logger.getLogger(PhotoServiceImpl.class.getName());
    private static PhotoServiceImpl singleton = new PhotoServiceImpl();

    private PhotoServiceImpl() {
    }

    public static PhotoServiceImpl getInstance() {
        return singleton;
    }

    public Photo getPhoto(String cid) {

        return (Photo) this.dao.get(Photo.class, cid);
    }

    public List queryPhotoByBlog(BlogInfo blog) {
        List photoList = new ArrayList();
        Collection<Object> params = new ArrayList<Object>();
        params.add(blog.getUsername());
        photoList = dao.query(Photo.class, "inputuser=?", params);
        return photoList;
    }

    public PhotoDir getPhotoDir(String cid) {
        return (PhotoDir) this.dao.get(PhotoDir.class, cid);
    }

    public boolean addPhoto(Photo photo) {
        boolean ret = false;
        photo.setInputtime(new java.util.Date());
        photo.setStatus(new Integer(0));
        ret = this.dao.save(photo);
        photo.setHtmlurl(photo.staticHtmlUrl());
        this.dao.update(photo);
        if (ret) {
            try {
                BlogInfo blog = BlogUserServiceImpl.getInstance().getBlogInfo(photo.getBlogcid());
                blog.setUpdated(Boolean.TRUE);
                HTMLGenerater.saveLeftPhotoDirToHTML(blog);//左侧最新相册
                HTMLGenerater.saveLeftPhotoToHTML(blog);//左侧最新照片
                HTMLGenerater.saveToHTML(photo);//生成混合相册页面了。
                HTMLGenerater.savePhotoDocToHtml(photo);//单个图片内容页。
                HTMLGenerater.savePhotoDIRToHTMLByBlog(blog, photo.getDirid());//生成用户单个相册页面
            } catch (Exception e) {
                logger.error(e);
            }
        }
        return ret;
    }

    public boolean addPhotoDir(PhotoDir photoDir) {
        boolean ret = false;
        photoDir.setInputtime(new java.util.Date());
        photoDir.setStatus(new Integer(0));
        ret = this.dao.save(photoDir);
        //重新生成相册首页和用户首页
        BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(photoDir.getInputuser());
        HTMLGenerater.saveLeftPhotoDirToHTML(blog);//左侧最新相册
        HTMLGenerater.savePhotoDIRToHTMLByBlog(blog, photoDir.getCid());//生成用户单个相册页面
        HTMLGenerater.savePhotoDirIndexToHTMLByBlog(blog);//生成用户相册首页
        HTMLGenerater.saveToHTML(blog);//然后重新生成该文章所有者的首页
        return ret;
    }

    public int batchDelPhoto(String[] cids) {
        int ret = 0;
        for (int i = 0; i < cids.length; i++) {
            if (delPhoto(cids[i])) {
                ret++;
            }
        }
        return ret;
    }

    public int batchDelPhotoDir(String[] cids) {
        int ret = 0;
        for (int i = 0; i < cids.length; i++) {
            if (delPhotoDir(cids[i])) {
                ret++;
            }
        }
        return ret;
    }

    public boolean delPhoto(String cid) {
        boolean ret = false;
        Photo photo=getPhoto(cid);
        ret = this.dao.del(photo);
        if (ret) {
            try {
                BlogInfo blog = BlogUserServiceImpl.getInstance().getBlogInfo(photo.getBlogcid());
                HTMLGenerater.saveLeftPhotoDirToHTML(blog);//左侧最新相册
                HTMLGenerater.saveLeftPhotoToHTML(blog);//左侧最新照片
                HTMLGenerater.saveToHTML(photo);//生成混合相册页面了。
                HTMLGenerater.savePhotoDocToHtml(photo);//单个图片内容页。
                HTMLGenerater.savePhotoDIRToHTMLByBlog(blog, photo.getDirid());//生成用户单个相册页面
            } catch (Exception e) {
                logger.error(e);
            }
        }
        return ret;
    }

    public boolean delPhotoDir(String cid) {
        boolean ret = false;
        PhotoDir photoDir=getPhotoDir(cid);
        ret = this.dao.del(photoDir);
        if (ret) {
            try {
                BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(photoDir.getInputuser());
                HTMLGenerater.saveLeftPhotoDirToHTML(blog);//左侧最新相册
                HTMLGenerater.savePhotoDIRToHTMLByBlog(blog, photoDir.getCid());//生成用户单个相册页面
                HTMLGenerater.savePhotoDirIndexToHTMLByBlog(blog);//生成用户相册首页
                HTMLGenerater.saveToHTML(blog);//然后重新生成该文章所有者的首页
            } catch (Exception e) {
                logger.error(e);
                //e.printStackTrace();
            }
        }
        return ret;
    }

    public IPageList queryPhoto(PhotoDir dir, int currentPage, int pageSize) {
        String scope = "dirid=? ";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(dir.getCid());
        return queryPhoto(scope, paras, currentPage, pageSize);
    }

    public IPageList queryPhotoAll(int currentPage, int pageSize) {
        String scope = "1=1 ";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        return queryPhoto(scope, paras, currentPage, pageSize);
    }

    public IPageList queryPhoto(String dir, String username, int currentPage, int pageSize) {
        String scope = "dirid=? and inputuser=? ";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(dir);
        paras.add(username);
        return queryPhoto(scope, paras, currentPage, pageSize);
    }

    public IPageList queryPhoto(String username, int currentPage,
            int pageSize) {
        String scope = "inputuser=? order by inputtime desc";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(username);
        return queryPhoto(scope, paras, currentPage, pageSize);
    }

    public IPageList queryPhotoByFieldName(String fieldName, String value, int currentPage,
            int pageSize) {
        String scope = fieldName + "=" + "?";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(value);
        return queryPhoto(scope, paras, currentPage, pageSize);
    }

    public IPageList queryCirclePhoto(String cid, int currentPage, int pageSize) {
        String scope = "circlesn=?";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(cid);
        return queryPhoto(scope, paras, currentPage, pageSize);
    }

    public IPageList queryPhoto(String scope, Collection paras, int currentPage, int pageSize) {
        DbPageList pList = new DbPageList(Photo.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public IPageList queryPhotoDir(String username,
            int currentPage, int pageSize) {
        String scope = "inputuser=? ";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(username);
        DbPageList pList = new DbPageList(PhotoDir.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public IPageList queryPhotoDir(int currentPage, int pageSize) {
        String scope = "1=1";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        DbPageList pList = new DbPageList(PhotoDir.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public IPageList queryChildPhotoDir(String parentsn, int currentPage, int pageSize) {
        String scope = "parentsn=? ";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(parentsn);
        DbPageList pList = new DbPageList(PhotoDir.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public boolean updatePhoto(Photo photo) {
        boolean ret = false;
        ret = this.dao.update(photo);
        if (ret) {
            try {
                BlogInfo blog = BlogUserServiceImpl.getInstance().getBlogInfo(photo.getBlogcid());
                HTMLGenerater.saveLeftPhotoDirToHTML(blog);//左侧最新相册
                HTMLGenerater.saveLeftPhotoToHTML(blog);//左侧最新照片
                HTMLGenerater.saveToHTML(photo);//生成混合相册页面了。
                HTMLGenerater.savePhotoDocToHtml(photo);//单个图片内容页。
                HTMLGenerater.savePhotoDIRToHTMLByBlog(blog, photo.getDirid());//生成用户单个相册页面
            } catch (Exception e) {
                System.out.println("修改照片保存页面是出错");
                logger.error(e);
            }
        }
        return ret;
    }

    public boolean updatePhotoDir(PhotoDir photoDir) {
        boolean ret = false;
        ret = this.dao.update(photoDir);

        BlogInfo blog = BlogUserServiceImpl.getInstance().getUserBlog(photoDir.getInputuser());
        HTMLGenerater.saveLeftPhotoDirToHTML(blog);//左侧最新相册
        HTMLGenerater.savePhotoDIRToHTMLByBlog(blog, photoDir.getCid());//生成用户单个相册页面
        HTMLGenerater.savePhotoDirIndexToHTMLByBlog(blog);//生成用户相册首页
        HTMLGenerater.saveToHTML(blog);//然后重新生成该文章所有者的首页
        return ret;
    }

    public void uploadPhoto(Photo photo, InputStream in) {
        // TODO Auto-generated method stub
    }

    public Photo readPhoto(String cid) {
        Photo photo = getPhoto(cid);
        System.out.println(photo.getCid());
        System.out.println(photo.getInputuser());
        System.out.println(photo.getUrl());
        System.out.println(photo.getInputuser());
        System.out.println(photo.getIntro());
        System.out.println(photo.getTitle());
        System.out.println(photo.getBlogcid());
        photo.setReadtimes(new Integer(photo.getReadtimes() != null ? (photo.getReadtimes().intValue() + 1) : 0));
        this.updatePhoto(photo);
        return photo;
    }

    public boolean removePhotoHtml(Photo photo) {
        String url = Globals.APP_BASE_DIR + "/" + photo.getUrl();
        String s_url = Globals.APP_BASE_DIR + "/" + photo.getS_url();
        String htmlUrl = Globals.APP_BASE_DIR + "/" + photo.getHtmlurl();
        return BlogFileUtil.removeFile(url) && BlogFileUtil.removeFile(s_url) && BlogFileUtil.removeFile(htmlUrl);
    }
}
